<?php

namespace app\api\model\Goods\Speci;

use app\api\model\Goods;
use app\api\model\Product;
use think\Model;
use think\Db;
trait WithGroup
{
    //use Speci;
    /**
     * @var Model
     */
    private $groupModel;

    public function getPidAttr($value)
    {
        return $this->groupModel::field('gid,gname as pid_name')->find($value);//['gname'];
    }

    public function getStatusAttr($value)
    {
        $status = [0=>'禁用',1=>'启用'];
        return $status[$value];
    }
	/**
     * @param mixed $id
     * @return array|mixed|\PDOStatement|string|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function info($id)
    {
        return self::find($id);
    }

    /**
     * @param $data
     * @return Unit
     */
    public function add($data)
    {
        return self::create($data);
    }

    /**
     * @param $where
     * @param $page
     * @param $size
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function index($where, $page, $size)
    {
        return self::where($where)->page($page, $size)->paginate($size);
//        return self::where($where)->page($page, $size)->paginate($size)->each(function ($item) {
//            $item['pid_name'] =  $this->groupModel::find($item['pid'])['gname'];
//            return $item;
//        });
    }

    /**
     * @param $id
     * @param $data
     * @return int|string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function edit($id, $data)
    {
        return self::where('id', $id)->update($data);
    }

    /**
     * @param $id
     * @return bool|int
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function del($id)
    {
        // 有绑定到商品则不能删除
        if ((Product::where(self::FIELD, $id)->count()) > 0) {
            return false;
        } else {
            return self::where('id', $id)->delete();
        }
    }

    /**
     * @param $id
     * @return array|\PDOStatement|string|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getGroup($id)
    {
        return $this->groupModel->where('level', self::LEVEL)->find(['gid' => $id]);
    }

    /**
     * @param $data
     * @return int|string
     */
    public function addGroup($data)
    {
        $data['level'] = self::LEVEL;
        return $this->groupModel::create($data);
    }

    public function getGroupSpec($level)
    {
        $list =$this->groupModel->where('level',$level)->select();
        foreach($list as $k=>$v){
            $list[$k]['son'] =Db::name('color3')->where('pid',$v['gid'])->select();
        }
        return $list;
    }

    /**
     * @param $where
     * @param $page
     * @param $size
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function indexGroup($where, $page, $size)
    {
        return $this->groupModel->where('level', self::LEVEL)
            ->where($where)->page($page, $size)->paginate($size);
    }

    /**
     * @param $id
     * @param $data
     * @return int|string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function editGroup($id, $data)
    {
        return $this->groupModel->where('level', self::LEVEL)
            ->where('gid', $id)->update($data);
    }

    /**
     * @param $id
     * @return int
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function deleteGroup($id)
    {
        return $this->groupModel->where('level', self::LEVEL)->delete(['gid' => $id]);
    }
}
